package com.dhcc.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.dhcc.utils.AuthUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 自定义字段填充逻辑
 * @ProjectName Dhcc-Cloud
 * @PackageName com.dhcc.config
 * @Title DhccMetaObjectHandler
 * @Date 2024/11/19 下午4:21
 * @Author LiuGuoting
 */
@Component
public class DhccMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        // 获取当前登录用户ID
        String userId = AuthUtil.getUserId();
        // 插入时填充创建时间、更新时间、创建人、更新人
        this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("createUser", userId, metaObject);
        this.setFieldValByName("updateUser", userId, metaObject);
        this.setFieldValByName("status", (byte) 0, metaObject);
        this.setFieldValByName("delFlag", (byte) 0, metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 获取当前登录用户ID
        String userId = AuthUtil.getUserId();
        // 更新时填充更新时间、更新人
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("updateUser", userId, metaObject);
    }
}